package com.example.demo.component;

import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.util.MimeTypeUtils;

/**
 * @program: parent
 * @description:
 * @author: dongyang.du
 * @create: 2020-03-06 14:57
 **/
@Component
public class KafkaComponent {

	public void send(MessageChannel mc, Object body, String topic, MessageHeaderAccessor accessor) {
		mc.send(MessageBuilder.withPayload(body).setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
				.setHeader(KafkaHeaders.TOPIC, topic).setHeaders(accessor).build());
	}
}
